package com.tomcat.utils;

import com.tomcat.mapper.Context;
import com.tomcat.mapper.Host;
import com.tomcat.mapper.Mapper;
import com.tomcat.mapper.Wrapper;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * desc: 解析web.xml配置文件，封装到Mapper对象中
 *
 * @author tengfei wang
 * @version 1.0
 * @date Created in 27/9/2020
 */
public class ParsingServer {

    private Mapper mapper = new Mapper();

    private Host host;

    private String appBase;

    public ParsingServer(String appBase, Host host) {
        this.appBase = appBase;
        this.host = host;
        mapper.addMappedHost(host);
    }

    public Mapper getMapper() {
        return mapper;
    }

    public void parsingContent() {
        File file = new File(appBase);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files == null || files.length <= 0) {
                return;
            }
            //扫描web-apps下所有的文件夹，一个文件夹其实就是一个项目
            for (File f : files) {
                try {
                    scanContext(f);
                } catch (IOException | DocumentException | IllegalAccessException | InstantiationException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void scanContext(File f) throws IOException, DocumentException, IllegalAccessException, InstantiationException {
        if (!f.isDirectory()) {
            return;
        }
        String name = f.getName();
        mapper.addContext(host.getHostName(), new Context(name));
        File canonicalFile = f.getCanonicalFile();
        File[] files = canonicalFile.listFiles();
        if (files == null || files.length <= 0) {
            return;
        }
        for (File file : files) {
            if ("web.xml".equalsIgnoreCase(file.getName())) {
                // 解析web.xml中的servlet文件
                SAXReader saxReader = new SAXReader();
                Document document = saxReader.read(file);
                Element rootElement = document.getRootElement();
                List<Element> servlets = rootElement.elements("servlet");
                for (Element servlet : servlets) {
                    Element servletNameElement = servlet.element("servlet-name");
                    String servletName = servletNameElement.getStringValue();
                    Element servletClassElement = servlet.element("servlet-class");
                    String servletClass = servletClassElement.getStringValue();
                    // 解析封装wrapper
                    List<Element> servletMappings = rootElement.elements("servlet-mapping");
                    for (Element servletMapping : servletMappings) {
                        Element element = servletMapping.element("servlet-name");
                        if (servletName.equalsIgnoreCase(element.getStringValue())) {
                            Element urlElement = servletMapping.element("url-pattern");
                            String urlPattern = urlElement.getStringValue();
                            // 通过自定义类加载器解析servlet，生成对象实例
                            MyClassLoader myClassLoader = new MyClassLoader();
                            // 添加servlet到mapper的wrapper中
                            mapper.addWrapper(host.getHostName(), name, new Wrapper(urlPattern, myClassLoader.findClass(appBase + name + "/", servletClass).newInstance()));
                        }
                    }

                }
            }
        }
    }
}
